package com.cz.pg;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeviceDataTaskInsert {
    public static void main(String[] args) {

        CronUtil.schedule("*/3 * * * * *", (Task) DeviceDataTaskInsert::doTask);

        // 设置秒级别的启用，默认是分钟级别
        CronUtil.setMatchSecond(true);

        // 启动定时器
        CronUtil.start();

    }

    public static void doTask() {
        System.out.println("数据采集任务已启动，每3秒执行一次...");
        String dbGroup = "pg";
        List<Entity> entityList = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            Entity entity = new Entity("device_data");
            entity.set("is_online", RandomUtil.randomBoolean());
            entity.set("device_id", i);
            entity.set("device_port", 8193);
            entity.set("nc_type", "FanucSeriesOi-MD");
            entity.set("part_count_total", RandomUtil.randomInt(0, 1000));
            entity.set("main_program_name", "NCProgram" + i);
            entity.set("current_tool", i);
            entity.set("spindle_override", RandomUtil.randomInt());
            entity.set("feedrate_override", RandomUtil.randomInt());
            entity.set("spindle_speed_actual", RandomUtil.randomInt());
            entity.set("spindle_feed_speed_actual", RandomUtil.randomInt());
            entity.set("run_time_total", RandomUtil.randomInt());
            entity.set("cut_time_total", RandomUtil.randomInt());
            entity.set("cycle_time", RandomUtil.randomInt());
            entity.set("get_power_on_time_total", RandomUtil.randomInt());
            entity.set("alarm_code", RandomUtil.randomInt());
            entity.set("alarm_msg", RandomUtil.randomInt());
            entity.set("is_run", RandomUtil.randomBoolean());
            entity.set("nc_mode", RandomUtil.randomString(5));
            entity.set("spindle_load_rate", RandomUtil.randomInt());

            try {
                Db.use(dbGroup).insert(entity);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
